﻿using MQ.Core.Common;
using MQ.Core.Db;
using MQ.Core.Db.SqlServer;
using MQ.Core.Entity.System;
using MQ.Core.IRepository.System;
using MQ.Core.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MQ.Core.Repository.System
{
    public class UserRepository : BaseRepository, IUserRepository
    {
        private readonly ISqlDb db;
        private readonly MqDbContext context;

        public UserRepository(ISqlDb db,MqDbContext context) :base(db)
        {
            this.db = db;
            this.context = context;
        }
        public void Add(UserEntity entity)
        {
            db.Insert(entity);
        }

        public Task AddAsync(UserEntity entity)
        {
            return db.InsertAsync(entity);
        }

        public override void Delete(string ids)
        {
            string sql = $"delete from sys_user where id in({ids})";
            db.Execute(sql);
        }

        public override Task DeleteAsync(string ids)
        {
            string sql = $"delete from sys_user where id in({ids})";
            return db.ExecuteAsync(sql);
        }

        public bool Exist(string query)
        {
            var q = context.User.AsQueryable();
            var json = query.ToJObject();
            if (!json["name"].IsEmpty())
            {
                q = q.Where(m => m.Name==json["name"].ToString());
            }
            if (!json["sex"].IsEmpty())
            {
                q = q.Where(m => m.Sex == json["name"].ToInt32());
            }
            return q.Count()>0;
        }

        public Task<bool> ExistAsync(string query)
        {
            throw new NotImplementedException();
        }

        public UserEntity GetEntity(long id, string query = null)
        {
            string sql = $"select * from sys_user where 1=1";
            return db.GetEntity<UserEntity>(sql);
        }

        public Task<UserEntity> GetEntityAsync(long id, string query = null)
        {
            string sql = $"select * from sys_user where 1=1";
            return db.GetEntityAsync<UserEntity>(sql);
        }

        public IEnumerable<UserEntity> GetList(string query)
        {
            var q = context.User.AsQueryable();
            var json = query.ToJObject();
            if (!json["name"].IsEmpty())
            {
                q = q.Where(m => m.Name.Contains(json["name"].ToString()));
            }
            if (!json["sex"].IsEmpty())
            {
                q = q.Where(m => m.Sex==json["name"].ToInt32());
            }
            return q.ToList();
        }

        public override IEnumerable<T> GetList<T>(string query)
        {
            throw new NotImplementedException();
        }

        public Task<IEnumerable<UserEntity>> GetListAsync(string query)
        {
            throw new NotImplementedException();
        }

        public override Task<IEnumerable<T>> GetListAsync<T>(string query)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<UserEntity> GetPageList(Pagination pagination, string query)
        {
            var q = context.User.AsQueryable();
            var json = query.ToJObject();
            if (!json["name"].IsEmpty())
            {
                q = q.Where(m => m.Name.Contains(json["name"].ToString()));
            }
            if (!json["sex"].IsEmpty())
            {
                q = q.Where(m => m.Sex == json["name"].ToInt32());
            }
            return q.ToPageList(pagination).ToList();
        }

        public override IEnumerable<T> GetPageList<T>(Pagination pagination, string query)
        {
            throw new NotImplementedException();
        }

        public Task<IEnumerable<UserEntity>> GetPageListAsync(Pagination pagination, string query)
        {
            throw new NotImplementedException();
        }

        public override Task<IEnumerable<T>> GetPageListAsync<T>(Pagination pagination, string query)
        {
            throw new NotImplementedException();
        }

        public void Update(UserEntity entity)
        {
            db.Update(entity);
        }

        public Task UpdateAsync(UserEntity entity)
        {
            return db.UpdateAsync(entity);
        }
    }
}
